﻿#include "mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    player = new QMediaPlayer(this);
    playlist = new QMediaPlaylist(this);

    player->setPlaylist(playlist);

    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    openBtn = new QPushButton("打开文件夹",this);
    playButton = new QPushButton("播放", this);
    pauseButton = new QPushButton("暂停", this);
    stopButton = new QPushButton("停止", this);
    nextButton = new QPushButton("下一曲", this);
    previousButton = new QPushButton("上一曲", this);
    muteButton = new QPushButton("静音", this);
    volumeSlider = new QSlider(Qt::Horizontal, this);
    volumeSlider->setRange(0,100);
    volumeSlider->setValue(50);
    player->setVolume(50);
    positionSlider = new QSlider(Qt::Horizontal, this);
    currentTimeLabel = new QLabel("00:00", this);
    totalTimeLabel = new QLabel("00:00", this);
    playlistWidget = new QListWidget(this);

    QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
    QHBoxLayout *controlLayout = new QHBoxLayout();
    QHBoxLayout *timeLayout = new QHBoxLayout();

    controlLayout->addWidget(openBtn);
    controlLayout->addWidget(playButton);
    controlLayout->addWidget(pauseButton);
    controlLayout->addWidget(stopButton);
    controlLayout->addWidget(previousButton);
    controlLayout->addWidget(nextButton);
    controlLayout->addWidget(muteButton);
    controlLayout->addWidget(volumeSlider);

    timeLayout->addWidget(currentTimeLabel);
    timeLayout->addWidget(positionSlider);
    timeLayout->addWidget(totalTimeLabel);

    mainLayout->addWidget(playlistWidget);
    mainLayout->addLayout(controlLayout);
    mainLayout->addLayout(timeLayout);

    connect(openBtn,&QPushButton::clicked,this,&MainWindow::openFolder);
    connect(playButton, &QPushButton::clicked, this, &MainWindow::play);
    connect(pauseButton, &QPushButton::clicked, this, &MainWindow::pause);
    connect(stopButton, &QPushButton::clicked, this, &MainWindow::stop);
    connect(nextButton, &QPushButton::clicked, this, &MainWindow::next);
    connect(previousButton, &QPushButton::clicked, this, &MainWindow::previous);
    connect(muteButton, &QPushButton::clicked, this, &MainWindow::mute);
    connect(volumeSlider, &QSlider::valueChanged, this, &MainWindow::setVolume);
    connect(positionSlider, &QSlider::sliderMoved, this, &MainWindow::setPosition);
    connect(player, &QMediaPlayer::positionChanged, this, &MainWindow::updatePosition);
    connect(player, &QMediaPlayer::durationChanged, this, &MainWindow::updateDuration);

    // openFolder();
}

MainWindow::~MainWindow()
{
}

void MainWindow::openFolder()
{
    QString folder = QFileDialog::getExistingDirectory(this, "打开文件夹");
    if (!folder.isEmpty()) {
        QDir dir(folder);
        QStringList filters;
        filters << "*.mp3" << "*.wav";
        QFileInfoList fileList = dir.entryInfoList(filters, QDir::Files);
        foreach (QFileInfo fileInfo, fileList) {
            playlist->addMedia(QUrl::fromLocalFile(fileInfo.absoluteFilePath()));
            playlistWidget->addItem(fileInfo.fileName());
        }
    }
}

void MainWindow::play()
{
    player->play();
}

void MainWindow::pause()
{
    player->pause();
}

void MainWindow::stop()
{
    player->stop();
}

void MainWindow::next()
{
    playlist->next();
}

void MainWindow::previous()
{
    playlist->previous();
}

void MainWindow::mute()
{
    player->setMuted(!player->isMuted());
}

void MainWindow::setVolume(int volume)
{
    player->setVolume(volume);
}

void MainWindow::updatePosition(qint64 position)
{
    positionSlider->setValue(position);
    QTime currentTime(0, (position / 60000) % 60, (position / 1000) % 60);
    currentTimeLabel->setText(currentTime.toString("mm:ss"));
}

void MainWindow::setPosition(int position)
{
    player->setPosition(position);
}

void MainWindow::updateDuration(qint64 duration)
{
    positionSlider->setRange(0, duration);
    QTime totalTime(0, (duration / 60000) % 60, (duration / 1000) % 60);
    totalTimeLabel->setText(totalTime.toString("mm:ss"));
}

